﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using Aandelendelen.News;

namespace Aandelendelen.Controls
{
	public partial class NewsControl : BaseUserControl
	{
		protected override void OnFirstHit(object sender, EventArgs e)
		{
			this.DisplayNews();
		}

		private void DisplayNews()
		{
			NewsItemCollection newsItems = NewsCache.GetNews();

			Color rowColor = Color.FromArgb(0xe0f0ff);
			Color alternateColor = Color.FromArgb(0xc0d0e8);

			for (int i = 0; i < newsItems.Count; i++)
			{
				NewsItem newsItem = newsItems[i];

				Panel row = new Panel();
				row.CssClass = "newsitem";

				row.BackColor = ((i & 1) == 0 ? rowColor : alternateColor);
				Label l = new Label();
				l.Text = newsItem.Date.ToString("ddd, dd-MMM-yyyy HH:mm");
				l.CssClass = "newsdate";

				row.Controls.Add(l);

				HyperLink link = new HyperLink();
				link.CssClass = "newstitle";
				link.NavigateUrl = "/ShowNewsItem.aspx?index=" + i.ToString();
				link.Text = newsItem.Title;
				link.Style.Add(HtmlTextWriterStyle.PaddingRight, "8px");
				row.Controls.Add(link);

				//only add tweet-buttons for items younger than 2 months
				if (DateTime.Now.AddMonths(-2) < newsItem.Date)
				{
					HyperLink tweetLink = new HyperLink();
					tweetLink.CssClass = "twitter-share-button";
					tweetLink.Attributes.Add("data-url", "http://www.aandelendelen.nl");
					tweetLink.Attributes.Add("data-text", "#aandelen AandelenDelen.nl: " + newsItem.Title);
					tweetLink.Attributes.Add("data-count", "none");
					tweetLink.Text = "Tweet";
					tweetLink.NavigateUrl = "http://twitter.com/share";
					row.Controls.Add(tweetLink);

					JavaScriptControl js = new JavaScriptControl();
					js.Src = "http://platform.twitter.com/widgets.js";
					row.Controls.Add(js);
				}

				this.Panel1.Controls.Add(row);
			}
		}
	}
}